#!/usr/bin/env python
# encoding: utf-8
'''
@author: liuchong
@contact: lucas@007vin.com
@software: garner
@file: banzhou.py
@time: 2020/9/9 2:44 下午
@desc:
'''

import sys
import re
import pandas as pd

sys.path.append('../../')
from models.porsches.porsches import CarsPartNumNew, CarsSaDesc
from models.porsches.porsches_vb import Vb

from scripts.fenci import remove_tt
from pymongo import MongoClient

vwag_parts = CarsPartNumNew.select(CarsPartNumNew.pid).where(
    (CarsPartNumNew.pid != '') & ((CarsPartNumNew.model != '') | (CarsPartNumNew.sa_code != ''))).distinct().order_by(CarsPartNumNew.pid)

s = ['2缸', '二缸', '3缸', '三缸', '4缸', '四缸', '5缸', '五缸', '6缸', '六缸', '8缸', '八缸', '10缸', '十缸', '12缸', '十二缸']
qd = ['全轮驱动', '前驱动', '前部驱动', '四轮驱动']
bsx = ['7档', '7档自动', '7档手动', '7档双离合', '七档', '七档自动', '七档手动', '七档双离合', '6档', '6档自动', '6档手动', '6挡手自一体',
       '六档', '六档自动', '六档手动', '5档', '5档自动', '5档手动', '五档', '五档自动', '五档手动', '5挡手自一体',
       '4档', '4档自动', '4档手动', '四档', '四档自动', '四档手动', '8档', '8档自动', '8档手动', '8挡手自一体',
       '八档', '八档自动', '八档手动', '3档', '3档自动', '3档手动', '三档', '三档自动', '三档手动', '无级自动']
pl = ['4.0', '2.0', '3.0', '3.3', '4.5', '4.7', '2.5', '5.0', '3.2', '2.8', '3.6', '2.7', '5.4', '3.7', '3.4', '5.7', '3.8', '4.8', '2.9', '4.2', '4.6', '1.8', '1.7', '1.1', '1.3', '1.5', '1.6', '2.2', '2.4']
fdjdm = ['DKD', 'DKDA', '0478', '0479', '047E', '047F', '3101', '3102', '3103', '3104', '3150', '93003', '93004', '93005', '93006', '93007', '93008', '93009', '93010', '93013', '93015', '93016', '93017', '93019', '93060', '93061', '93062', '93063', '93064', '93065', '93066', '2801', '2802', '2803', '2804', '2809', '2810', '2811', '2812', '2813', '2814', '2815', '2816', '2817', '2818', '0474', '0475', '0476', '4401', '4402', '4403', '4404', '2819', '2820', '2821', '2822', '2843', '2844', '2845', '2846', '93020', '93021', '93026', '93068', '4405', '4406', '4407', '4408', '4409', '4410', '4440', '4450', '4451', '4452', '95950', '2841', '2842', '2847', '93025', '3069', '6401', '6402', '6403', '6450', '4411', '4412', '4441', '4443', '4444', '2849', '2850', '6405', '6406', '6407', '6408', '6420', '6421', '6422', '6423', '6424', '6460', '9620', '9621', '9622', '9623', '9624', '9601', '9602', '9603', '9604', '9676', '9679', '9670', '9670E', '9670S', '022Y', '4800', '4850', '8001', '9605', '9701', '9701S', '9625', '9626', '9720', '9721', '9722', '9774', '9776', '9777', '9777R', '9770', '9770S', '059D', '4801', '4851', '5501', 'A101', 'A102', 'A120', 'A121', '4620', '4640', '4820', '4840', '4870', 'CGE', 'CGEA', 'CGFA', 'CRC', 'CRCB', 'CRCC', 'CWAA', 'CWB', 'CWBA', 'CWC', 'CWCA', 'CWD', 'CWDA', 'CWF', 'CWFA', 'CWJA', 'CXNA', 'CXPA', 'CXR', 'CXRA', 'A170', '059E', '06EC', '4802', '4852', '5502', 'CEYA', 'CFT', 'CFTB', 'CJT', 'CJTB', 'CNRB', 'CRCA', 'CUDB', 'CUDC', 'CUR', 'CURA', 'CVVA', 'CVVB', 'CVVC', 'CXZ', 'CXZA', 'CYX', 'CYXA', 'A103', 'A104', 'A122', 'A123', 'A175', 'A176', 'DGG', 'DGGA', 'DGGB', 'DNS', 'DNSA', 'A171', 'A172', 'DAB', 'DABA', 'DBC', 'DBCA', 'DBCB', 'DHNA', '1800', 'CDUD', 'CNC', 'CNCC', 'CPNB', 'CTBA', 'CTBB', 'CTBC', 'CTL', 'CTLA', 'CTM', 'CTMA', 'CYN', 'CYNA', 'CYNB', 'CYP', 'CYPA', 'DCN', 'DCNA', 'DHK', 'DHKA', 'DLH', 'DLHB', 'DBXA', 'DBXB', 'AB03', 'AB04', 'DCH', 'DCHA', 'DCJ', 'DCJA', 'DCK', 'DCKA', 'DPE', 'DPEA', 'DDN', 'DDNC', 'DDP', 'DDPA', 'DDPB', 'DJU', 'DJUA', 'DJUB', 'DKDC', 'DKDD', 'DNY', 'DNYA', 'DNZ', 'DNZA', 'DNZB', 'DNZC', 'DPG', 'DPGA', 'DPGB', 'DPH', 'DPHA', 'DPHB', 'CSZ', 'CSZA', 'CVD', 'CVDA', 'CVDD', 'CXT', 'CXTA', 'DBUC', 'DEPA', 'DEPB', 'DERA', 'DERC', 'DGP', 'DGPA', 'DLZ', 'CZA', 'CZAA', 'DCA', 'DCAB', 'DCB', 'DCBE', 'DCU', 'DCUA', 'DFK', 'DFKA', 'DGR', 'DGRC', 'DKN', 'DKNA', 'DLZB', 'DKC', 'DKCA', 'DKK', 'DKKA', 'DRT', 'DRTA', 'DRV', 'DRVA', 'EBE', 'EBEA', 'EBF', 'EBFA', 'EBG', 'EBGA', 'EBGB', '021', '022', '039', '90136', '90137', '90138', '90139', '369', '506', '5061', '5062', '508', '527', '528', '5282', '546', '5462', '5471', '589', '5892', '6161', '6162', '6920', '6921', '6922', '5871', '5872', '61612', '61615', '61616', '6167', '6923', '6923A', '61636', '61639', '61640', '90101', '90102', '90103', '90105', '90106', '90107', '90108', '90109', '90110', '90111', '90113', '90114', '90116', '90117', '90119', '91101', '91102', '91103', '91104', '91106', '91107', '91108', '91151', '91152', '91153', '91157', '91161', '91162', '91163', '91167', '91183', '91191', '91196', 'DKE', 'DKH', '91141', '91142', '91143', '91144', '91146', '91147', '91148', '91149', '91181', '91182', '91184', '91185', '91186', '91189', '91190', '91192', '91193', '91194', '91197', '91198', '91199', '92302', '93002', '93012', '93050', '93051', '93052', '93053', '93054']
client = MongoClient('192.168.10.170', 27017)
db = client.standard_label
collection = db.standard_label_unique
item = []
flag = 0
for a in vwag_parts:
    # 配置代码
    sa_codes = []
    # 发动机排量
    cylinder = []
    # 发动机代码
    vb = []
    # 发动机排量信息
    vb_desc = []

    # model_desc = []
    transmission = []
    drive = []
    diff_mech = []
    # 匹配信息
    label_info = CarsPartNumNew.get_or_none(CarsPartNumNew.pid == a.pid)
    label = label_info.label
    if label:
        label = re.findall(r'[\u4e00-\u9fa5]', label)
        label = ''.join(label)
    stand_info = collection.find_one({'pid': a.pid, 'brandCode': 'porsches'})
    if stand_info:
        stand_label = stand_info['standard_label']
    else:
        stand_label = ''
    # 处理audi_copy表中的label字段
    parts = CarsPartNumNew.select().where(CarsPartNumNew.pid == a.pid)
    for part in parts:
        cid = part.cid

        sa_codes += part.sa_code.split(',')
        part_remark = part.remark or ''

        part_model = part.model
        part_model = part_model.replace('<br/>', '')

        # 筛选出structure表model字段中的缸数和排量信息
        part_out = remove_tt(part_model)
        part_remark_out = remove_tt(part_remark)

        if part_remark:
            for ind, key in enumerate(s):
                if key in part_remark_out:
                    key = key.replace('二缸', '2')
                    key = key.replace('2缸', '2')
                    key = key.replace('三缸', '3')
                    key = key.replace('3缸', '3')
                    key = key.replace('四缸', '4')
                    key = key.replace('4缸', '4')
                    key = key.replace('五缸', '5')
                    key = key.replace('5缸', '5')
                    key = key.replace('六缸', '6')
                    key = key.replace('6缸', '6')
                    key = key.replace('八缸', '8')
                    key = key.replace('8缸', '8')
                    key = key.replace('十缸', '10')
                    key = key.replace('10缸', '10')
                    key = key.replace('十二缸', '12')
                    key = key.replace('12缸', '12')
                    cylinder.append(key)

            for key in pl:
                if key in part_remark:
                    vb_desc.append(key)
        if part_model:
            for key in bsx:
                if key in part_out:
                    key = key.replace('三', '3')
                    key = key.replace('四', '4')
                    key = key.replace('五', '5')
                    key = key.replace('六', '6')
                    key = key.replace('七', '7')
                    key = key.replace('八', '8')
                    key = key.replace('档', '挡')
                    if key == '5挡':
                        key = '5挡手动'
                    if key == '6挡':
                        key = '6挡手动'
                    if key == '6挡自动':
                        key = '6挡AMT'

                    if key == '7挡自动':
                        key = '7挡双离合'

                    if key == '无级自动':
                        key = 'CVT无级变速'

                    transmission.append(key)
            for key in qd:
                if key in part_out:
                    key = key.replace('前驱动', '前置前驱')
                    key = key.replace('前部驱动', '前置前驱')
                    key = key.replace('四轮驱动', '全轮驱动')
                    drive.append(key)

            for ind, key in enumerate(s):
                if key in part_out:
                    key = key.replace('二缸', '2')
                    key = key.replace('2缸', '2')
                    key = key.replace('三缸', '3')
                    key = key.replace('3缸', '3')
                    key = key.replace('四缸', '4')
                    key = key.replace('4缸', '4')
                    key = key.replace('五缸', '5')
                    key = key.replace('5缸', '5')
                    key = key.replace('六缸', '6')
                    key = key.replace('6缸', '6')
                    key = key.replace('八缸', '8')
                    key = key.replace('8缸', '8')
                    key = key.replace('十缸', '10')
                    key = key.replace('10缸', '10')
                    key = key.replace('十二缸', '12')
                    key = key.replace('12缸', '12')
                    cylinder.append(key)
            for key in pl:
                if key in part_model:
                    vb_desc.append(key)
            if not part_model.startswith('PR:') and not part_model.startswith('PR-') and not part_model.startswith('pr:') and not part_model.startswith('pr-'):
                for key in fdjdm:
                    if key in part_model:
                        vb.append(key)
            if 'PR:' in part_model or 'PR-' in part_model or 'pr:' in part_model or 'pr-' in part_model:
                # 处理part_model中的PR
                part_pr = re.sub('[\u4e00-\u9fa5]', '', part_model)
                part_pr = part_pr.replace('PR:', '')
                part_pr = part_pr.replace('PR-', '')
                part_pr = part_pr.replace('+', ',')
                part_pr = part_pr.replace('/', ',')
                part_pr = part_pr.replace(':', ',')
                part_pr = part_pr.replace('_', ',')
                part_pr = part_pr.split(',')
                for j in part_pr:
                    sa_codes.append(j)

    sa_codes = set(sa_codes)
    sa_codes = list(sa_codes)

    vb = set(vb)
    vb = list(vb)
    # print(vb)

    # 筛选sa_code对应的描述信息
    for i in sa_codes:
        contrast = CarsSaDesc.get_or_none(CarsSaDesc.sa == i, CarsSaDesc.lang == 'zh', CarsSaDesc.brand_code == 'porsches')
        if contrast and contrast.desc:
            # model_desc.append(contrast.desc)
            if '发动机' in contrast.desc:
                # 筛选出sa表desc中的排量信息
                for key in pl:
                    if key in contrast.desc:
                        vb_desc.append(key)
                for key in s:
                    if key in contrast.desc:
                        key = key.replace('二缸', '2')
                        key = key.replace('2缸', '2')
                        key = key.replace('三缸', '3')
                        key = key.replace('3缸', '3')
                        key = key.replace('四缸', '4')
                        key = key.replace('4缸', '4')
                        key = key.replace('五缸', '5')
                        key = key.replace('5缸', '5')
                        key = key.replace('六缸', '6')
                        key = key.replace('6缸', '6')
                        key = key.replace('八缸', '8')
                        key = key.replace('8缸', '8')
                        key = key.replace('十缸', '10')
                        key = key.replace('10缸', '10')
                        key = key.replace('十二缸', '12')
                        key = key.replace('12缸', '12')
                        cylinder.append(key)

            # 处理变速器配置
            if ('变速箱' in contrast.desc or '变速器' in contrast.desc) and ('档' in contrast.desc or '无级' in contrast.desc):
                for key in bsx:
                    if key in contrast.desc:
                        key = key.replace('三', '3')
                        key = key.replace('四', '4')
                        key = key.replace('五', '5')
                        key = key.replace('六', '6')
                        key = key.replace('七', '7')
                        key = key.replace('八', '8')
                        key = key.replace('档', '挡')
                        if key == '5挡':
                            key = '5挡手动'
                        if key == '6挡':
                            key = '6挡手动'

                        if key == '6挡自动':
                            key = '6挡AMT'

                        if key == '7挡自动':
                            key = '7挡双离合'

                        if key == '无级自动':
                            key = 'CVT无级变速'
                        transmission.append(key)

            # 处理驱动配置
            for key in qd:
                if key in contrast.desc:
                    key = key.replace('前驱动', '前置前驱')
                    key = key.replace('前部驱动', '前置前驱')
                    key = key.replace('四轮驱动', '全轮驱动')
                    drive.append(key)

            # 处理差速器配置
            if '差速' in contrast.desc or '减速' in contrast.desc:
                diff_mech.append(i)

    # 筛选model中发动机代码对应的排量信息
    for i in vb:
        # print(Vb.aa.zfill(3))
        # break
        if cid.isdigit():
            cid = str(int(cid))
        vb_AF = Vb.get_or_none(Vb.ab == i, Vb.brand == 'porsches')
        if vb_AF and vb_AF.af:
            # print(int(vb_AF.af)/100)
            # break
            vb_desc.append(str(int(vb_AF.af)/100))

    # print(vb)
    vb_desc = set(vb_desc)
    vb_desc = list(vb_desc)
    vb_desc = ','.join(vb_desc)

    cylinder = set(cylinder)
    cylinder = list(cylinder)
    cylinder = ','.join(cylinder)

    transmission = set(transmission)
    transmission = list(transmission)
    transmission = ','.join(transmission)

    drive_info = []
    drive = set(drive)
    drive = list(drive)
    for i in drive:
        if i == '前置前驱':
            drive_info.append('两驱')
        if i == '全轮驱动':
            drive_info.append('四驱')
    drive_new = ','.join(drive_info)
    drive = ','.join(drive)

    diff_mech = set(diff_mech)
    diff_mech = list(diff_mech)
    diff_mech = ','.join(diff_mech)

    # model_desc = set(model_desc)
    # model_desc = list(model_desc)
    # model_desc = ','.join(model_desc)
    vb = ','.join(vb)
    unit = {'pid': a.pid, 'standard_label': stand_label, 'label': label, 'desc': '', 'engine_code': vb,
             'cylinder': cylinder, 'transmission': transmission, 'trans_desc': transmission, 'replace': '', 'drive': drive,
             '差速器': diff_mech, 'displacement': vb_desc, 'brandCode':'porsches', 'drive_new':drive_new}
    print(unit)
    item.append(unit)

df = pd.DataFrame(item)
df['pid'] = df['pid'].astype('str')
df.to_csv('./export/porsches_all01.csv', mode='a', index=False)